package com.template.app.model;

import java.util.Date;
import java.util.HashSet;
import java.util.Set;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.JoinTable;
import javax.persistence.ManyToMany;
import javax.persistence.ManyToOne;
import javax.persistence.NamedQueries;
import javax.persistence.NamedQuery;
import javax.persistence.Table;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;
import javax.persistence.Transient;
import javax.validation.constraints.Size;

import org.codehaus.jackson.annotate.JsonIgnore;
import org.codehaus.jackson.annotate.JsonProperty;
import org.hibernate.validator.constraints.NotBlank;

import com.template.app.model.util.Utils;

@Entity
@Table(name = "TEMPLATE")
@NamedQueries({
	@NamedQuery(name = Template.GET_FETCH,
			query = "select t from Template t " +
					"left join fetch t.templateRef " +
					"where t.id = :id"),
	@NamedQuery(name = Template.LIST, 
			query = "select t from Template t " +
					"order by t.name"),
	@NamedQuery(name = Template.LIST_BY_TEMPLATE_REF, 
			query = "select t from Template t " +
					"where t.templateRef.id = :id " +
					"order by t.name")
})
public class Template implements java.io.Serializable, Identifiable {

	private static final long serialVersionUID = 1L;

	public static final String NAME = "templates";
	
	public static final String GET_FETCH = NAME + ".getFetch";
	public static final String LIST = NAME + ".list";
	public static final String LIST_BY_TEMPLATE_REF = NAME + ".listByTemplateRef";
	
	private Integer id;
	private String name;
	private Date startDate;
	private TemplateRef templateRef;
	private Set<TemplateMany> templateManys = new HashSet<TemplateMany>();
	
	@Id
	@GeneratedValue(strategy = GenerationType.IDENTITY)
	@Column(name = "ID")
	public Integer getId() {
		return id;
	}
	
	public void setId(Integer id) {
		this.id = id;
	}
	
	@NotBlank
	@Size(max = 45)
	@Column(name = "NAME")
	public String getName() {
		return name;
	}
	
	public void setName(String name) {
		this.name = name;
	}

	@Temporal(TemporalType.TIMESTAMP)
	@Column(name = "START_DATE")
	public Date getStartDate() {
		return startDate;
	}

	public void setStartDate(Date startDate) {
		this.startDate = startDate;
	}

	@JsonIgnore
	@ManyToOne(fetch = FetchType.LAZY)
	@JoinColumn(name = "ID_TEMPLATE_REF")
	public TemplateRef getTemplateRef() {
		return templateRef;
	}

	public void setTemplateRef(TemplateRef templateRef) {
		this.templateRef = templateRef;
	}
	
	@JsonProperty("templateRef")
	@Transient
	public Integer getTemplateRefId() {
		return templateRef == null ? null : templateRef.getId();
	}

	@JsonIgnore
	@ManyToMany
	@JoinTable(
		name = "ASSOC_TEMPLATE_TEMPLATE_MANY",
		joinColumns = {@JoinColumn(name = "ID_TEMPLATE")},
		inverseJoinColumns = {@JoinColumn(name = "ID_TEMPLATE_MANY")}
	)
	public Set<TemplateMany> getTemplateManys() {
		return templateManys;
	}

	public void setTemplateManys(Set<TemplateMany> templateManys) {
		this.templateManys = templateManys;
	}

	@JsonIgnore
	//@JsonProperty("templateManys")
	@Transient
	public String getTemplateManysIds() {
		return templateManys == null ? null : Utils.setToCommaDelimitedStringId(templateManys);
	}

	@Override
	public String toString() {
		return name;
	}
	
	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = prime * result + ((id == null) ? 0 : id.hashCode());
		return result;
	}

	@Override
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (obj == null)
			return false;
		if (getClass() != obj.getClass())
			return false;
		Template other = (Template) obj;
		if (id == null) {
			if (other.id != null)
				return false;
		} else if (!id.equals(other.id))
			return false;
		return true;
	}

	@Transient
	public boolean isNew() {
		return id == null;
	}

}
